<?php
/**
 * 简化的兑换码API测试脚本
 * 通过HTTP请求测试API端点
 */

// 测试配置
$baseUrl = 'http://localhost'; // 根据实际情况调整
$apiPath = '/api/redemption_code';

echo "=== 兑换码API端点测试 ===\n\n";

// 测试函数
function testApiEndpoint($url, $data = [], $method = 'POST') {
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    
    if ($method === 'POST') {
        curl_setopt($ch, CURLOPT_POST, true);
        if (!empty($data)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        }
    }
    
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error = curl_error($ch);
    
    curl_close($ch);
    
    return [
        'http_code' => $httpCode,
        'response' => $response,
        'error' => $error
    ];
}

// 测试用例
$tests = [
    [
        'name' => '验证有效兑换码',
        'url' => $baseUrl . $apiPath . '/verify',
        'data' => ['code' => 'TEST123456'], // 需要实际存在的兑换码
        'expected_code' => 200
    ],
    [
        'name' => '验证无效兑换码',
        'url' => $baseUrl . $apiPath . '/verify',
        'data' => ['code' => 'INVALID123'],
        'expected_code' => 200
    ],
    [
        'name' => '批量验证兑换码',
        'url' => $baseUrl . $apiPath . '/batchVerify',
        'data' => ['codes' => json_encode(['TEST123456', 'INVALID123'])],
        'expected_code' => 200
    ],
    [
        'name' => '使用兑换码（未登录）',
        'url' => $baseUrl . $apiPath . '/use',
        'data' => ['code' => 'TEST123456'],
        'expected_code' => 200
    ],
    [
        'name' => '查询使用历史（未登录）',
        'url' => $baseUrl . $apiPath . '/history',
        'data' => [],
        'expected_code' => 200
    ]
];

// 执行测试
foreach ($tests as $index => $test) {
    echo ($index + 1) . ". {$test['name']}\n";
    
    $result = testApiEndpoint($test['url'], $test['data']);
    
    if (!empty($result['error'])) {
        echo "✗ CURL错误: {$result['error']}\n";
        continue;
    }
    
    echo "  HTTP状态码: {$result['http_code']}\n";
    
    if ($result['http_code'] === $test['expected_code']) {
        echo "✓ HTTP状态码正确\n";
    } else {
        echo "✗ HTTP状态码错误，期望: {$test['expected_code']}\n";
    }
    
    // 尝试解析JSON响应
    $jsonData = json_decode($result['response'], true);
    if ($jsonData !== null) {
        echo "  响应格式: JSON\n";
        echo "  响应代码: " . ($jsonData['code'] ?? 'N/A') . "\n";
        echo "  响应消息: " . ($jsonData['msg'] ?? 'N/A') . "\n";
        
        if (isset($jsonData['data'])) {
            echo "  数据字段: 存在\n";
        }
    } else {
        echo "  响应格式: 非JSON或解析失败\n";
        echo "  响应内容: " . substr($result['response'], 0, 200) . "...\n";
    }
    
    echo "\n";
}

echo "=== API端点测试完成 ===\n";

// 检查API路由配置
echo "\n检查API路由配置:\n";

$routeFiles = [
    '/www/wwwroot/www.jiuxin.com/app/api/route.php',
    '/www/wwwroot/www.jiuxin.com/route/api.php',
    '/www/wwwroot/www.jiuxin.com/config/route.php'
];

foreach ($routeFiles as $file) {
    if (file_exists($file)) {
        echo "✓ 找到路由文件: $file\n";
        $content = file_get_contents($file);
        if (strpos($content, 'redemption') !== false || strpos($content, 'RedemptionCode') !== false) {
            echo "  - 包含兑换码相关路由\n";
        } else {
            echo "  - 未找到兑换码相关路由\n";
        }
    }
}

// 检查控制器文件
echo "\n检查控制器文件:\n";
$controllerFile = '/www/wwwroot/www.jiuxin.com/app/api/controller/RedemptionCode.php';
if (file_exists($controllerFile)) {
    echo "✓ API控制器文件存在: $controllerFile\n";
    
    $content = file_get_contents($controllerFile);
    $methods = ['verify', 'batchVerify', 'use', 'history'];
    
    foreach ($methods as $method) {
        if (strpos($content, "function $method") !== false) {
            echo "  - ✓ 方法 $method 存在\n";
        } else {
            echo "  - ✗ 方法 $method 不存在\n";
        }
    }
} else {
    echo "✗ API控制器文件不存在: $controllerFile\n";
}